package com.im.business.history.service;

import com.alibaba.fastjson.JSONObject;
import com.im.business.history.entity.MessageHistory;
import com.im.business.history.domain.MessageHistoryVO;
import com.im.business.history.mapper.MessageHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Created leijun
 * @Date 2021/10/27 7:11 上午
 */
@Service
public class SingleMessageService {

    @Autowired
    private MessageHistoryMapper messageHistoryMapper;

    /**
     * 消息保存到数据库
     * @param message
     * @return
     */
    public void saveSingleMessage(JSONObject message) {
        MessageHistoryVO messageHistoryVO = new MessageHistoryVO();
        messageHistoryVO.setId(message.getLong("messageId"));
        messageHistoryVO.setSenderId(message.getLong("senderId"));
        messageHistoryVO.setReceiverId(message.getLong("receiverId"));
        messageHistoryVO.setGroupId(-1L);
        messageHistoryVO.setContent(message.getString("content"));
        messageHistoryVO.setRequestType(message.getInteger("requestType"));
        messageHistoryVO.setMessageType(message.getInteger("messageType"));
        messageHistoryVO.setSequence(message.getInteger("sequence"));
        messageHistoryVO.setSendTime(message.getLong("timestamp"));
        messageHistoryMapper.insert(messageHistoryVO);
    }

    /**
     * 查询单聊历史消息
     * @param senderId
     * @param receiverId
     * @param maxOffset
     * @return
     */
    public List<MessageHistory> selectHistoryMessage(Long senderId, Long receiverId, Long maxOffset) {
        return messageHistoryMapper.selectSingleHistoryMessage(senderId, receiverId, maxOffset);
    }
}
